<template>
  <div class="warp">
    <div class="warp_auto">
      <div class="warp_top">
        <div class="jiansuo-box">
          <span class="label">类型：</span>
          <el-select
            v-model="leixin"
            placeholder="请选择"
            clearable
            class="input"
          >
            <el-option
              v-for="item in optionsleixin"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <div class="jiansuo-box">
          <span class="label">状态：</span>
          <el-select
            v-model="value"
            placeholder="请选择状态"
            clearable
            class="input"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <el-button
          type="primary"
          icon="el-icon-search"
          class="btn"
          @click="btn"
        >检索</el-button>
      </div>

      <el-card>
        <el-table
          :data="arr"
          style="width: 100%"
          height="585"
          stripe
          :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
        >
          <el-table-column label="任务号" fixed  align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.task_name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="类型" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.type }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态"  align="center">
            <template slot-scope="scope">
            <span>{{
                scope.row.status === 1
                  ? "正常执行"
                  : scope.row.status === 2
                    ? "等待"
                    : scope.row.status === 3
                      ? "阻塞"
                      : scope.row.status === 4
                        ? "错误"
                        : "暂停"
              }}</span>
            </template>
          </el-table-column>

          <el-table-column label="上次执行时间" align="center">
            <template slot-scope="scope">
            <span style="margin-left: 10px">{{
                scope.row.start_time.substring(0, 19)
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="下次执行时间" align="center">
            <template slot-scope="scope">
            <span style="margin-left: 10px">{{
                scope.row.next_time.substring(0, 19)
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="计划" align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.cron }}</span>
            </template>
          </el-table-column>
          <el-table-column label="描述"  align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.description }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="280" align="center">
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="日志"
                placement="bottom"
              >
                <i
                  class="el-icon-tickets"
                  style="margin-right: 10px; font-size: 20px; padding-right: 10px"
                  @click="rizhi(scope)"
                />
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="编辑"
                placement="bottom"
              >
                <i
                  class="el-icon-edit"
                  style="margin-right: 10px; font-size: 20px; padding-right: 10px"
                  @click="handleEdit(scope.row)"
                />
              </el-tooltip>
              <el-popover
                :ref="`popover-${scope.$index}`"
                trigger="click"
                placement="top"
                width="100"
              >
                <p class="el-icon-warning">确定删除吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button
                    size="mini"
                    type="text"
                    @click="no_dele(scope)"
                  >取消</el-button>
                  <el-button
                    type="primary"
                    size="mini"
                    @click="yes_dele(scope)"
                  >确定</el-button>
                </div>
                <el-tooltip
                  slot="reference"
                  class="item"
                  effect="dark"
                  content="删除"
                  placement="bottom-start"
                >
                <span><i
                  class="el-icon-delete"
                  style="
                      margin-right: 10px;
                      font-size: 20px;
                      padding-right: 10px;
                    "
                /></span>
                </el-tooltip>
              </el-popover>

              <el-popover
                :ref="`popover-${scope.$index}`"
                trigger="click"
                placement="top"
                width="100"
              >
                <p class="el-icon-warning">确认暂停该任务吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button
                    size="mini"
                    type="text"
                    @click="no_stop(scope)"
                  >取消</el-button>
                  <el-button
                    type="primary"
                    size="mini"
                    @click="yes_stop(scope)"
                  >确定</el-button>
                </div>
                <el-tooltip
                  slot="reference"
                  class="item"
                  effect="dark"
                  content="暂停"
                  placement="bottom-start"
                >
                <span><i
                  class="el-icon-video-pause"
                  style="
                      margin-right: 10px;
                      font-size: 20px;
                      padding-right: 10px;
                    "
                /></span>
                </el-tooltip>
              </el-popover>
              <!--  -->
              <el-popover
                :ref="`popover-${scope.$index}`"
                trigger="click"
                placement="top"
                width="100"
              >
                <p class="el-icon-warning">确认恢复该任务吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button
                    size="mini"
                    type="text"
                    @click="no_play(scope)"
                  >取消</el-button>
                  <el-button
                    type="primary"
                    size="mini"
                    @click="yes_play(scope)"
                  >确定</el-button>
                </div>
                <el-tooltip
                  slot="reference"
                  class="item"
                  effect="dark"
                  content="恢复"
                  placement="bottom-start"
                >
                <span><i
                  class="el-icon-video-play"
                  style="
                      margin-right: 10px;
                      font-size: 20px;
                      padding-right: 10px;
                    "
                /></span>
                </el-tooltip>
              </el-popover>
              <!--  -->
              <el-popover
                :ref="`popover-${scope.$index}`"
                trigger="click"
                placement="top"
                width="100"
              >
                <p class="el-icon-warning">确认立即执行该任务吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button
                    size="mini"
                    type="text"
                    @click="no_est(scope)"
                  >取消</el-button>
                  <el-button
                    type="primary"
                    size="mini"
                    @click="yes_est(scope)"
                  >确定</el-button>
                </div>
                <el-tooltip
                  slot="reference"
                  class="item"
                  effect="dark"
                  content="立即执行"
                  placement="bottom-start"
                >
                <span><i
                  class="el-icon-refresh-right"
                  style="
                      margin-right: 10px;
                      font-size: 20px;
                      padding-right: 10px;
                    "
                /></span>
                </el-tooltip>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <el-dialog
        title="日志列表"
        :visible.sync="centerDialogVisible"
        :close-on-click-modal="false"
        width="770px"
        class="log"
        custom-class="role-mask"
      >
        <div style="height: 585px">
          <el-table :data="arrlist" style="width: 100%" height="528">
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-form-item label="出错原因：">
                    <span>{{ props.row.name }}</span>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column
              label="序号"
              type="index"
              width="50"
              align="center"
            />
            <el-table-column
              label="开始时间"
              prop="start_time"
              width="180"
              align="center"
            />
            <el-table-column
              label="完成时间"
              prop="end_time"
              width="180"
              align="center"
            />
            <el-table-column label="执行时长" width="120" align="center">
              <template slot-scope="props">
                {{ props.row.time_length + "秒" }}
              </template>
            </el-table-column>
            <el-table-column label="状态" width="120" align="center">
              <template slot-scope="scope">
                <span style="color: #19be6b">{{
                  scope.row.status === 1 ? "成功" : "失败"
                }}</span>
              </template>
            </el-table-column>
          </el-table>

          <div class="paging">
            <el-pagination
              :current-page="start"
              :page-size="PageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :pager-count="5"
              :total="totalCount"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </el-dialog>

      <!-- 编辑 -->
      <el-dialog
        title="修改任务"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        class="mod_box"
        custom-class="role-mask"
      >
        <el-form
          ref="infos"
          :model="infos"
          :rules="rules"
          label-width="90px"
          class="demo-ruleForm"
        >
          <el-form-item label="任务号：" prop="task_name">
            <el-input
              v-model="infos.task_name"
              :disabled="true"
              class="el-inputer"
            />
          </el-form-item>
          <el-form-item label="类型：" prop="type">
            <el-input
              v-model="infos.type"
              :disabled="true"
              class="el-inputer"
            />
          </el-form-item>
          <el-form-item label="计划：" prop="cron">
            <el-input v-model="infos.cron" class="el-inputer" />
          </el-form-item>
          <el-form-item label="描述：">
            <el-input
              v-model="infos.description"
              type="textarea"
              :rows="3"
              class="el-inputer"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="(dialogVisible = false), submitForm('infos')"
          >保 存</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import {
  quartzTaskGetList,
  quartzTaskUpdate,
  quartzTimeQueryList,
  quartzEdit
} from '@/api/table'
import { MessageBox, Message } from 'element-ui'

export default {
  data() {
    return {
      infos: {
        task_name: '',
        type: '',
        cron: ''
      },
      rules: {
        task_name: [
          { required: true, message: '任务号不能为空', trigger: 'blur' }
        ],
        type: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
        cron: [{ required: true, message: '计划不能为空', trigger: 'blur' }]
      },
      centerDialogVisible: false,
      // 控制弹窗 显示
      dialogVisible: false,
      // 点击查看按钮  这条数据详细信息
      dialogInfo: {},
      listarr: null,
      listLoading: true,
      loading: false,
      keshiname: '',
      dialogFormVisible: false,
      del: false,
      ruleForm: {
        bedCode: '',
        bedName: '',
        status: ''
      },
      options: [
        {
          id: '0',
          value: '2',
          label: '等待'
        },
        {
          id: '1',
          value: '0',
          label: '暂停'
        },
        {
          id: '2',
          value: '1',
          label: '正常执行'
        },
        {
          id: '3',
          value: '3',
          label: '阻塞'
        },
        {
          id: '4',
          value: '4',
          label: '错误'
        }
      ],
      optionsleixin: [
        {
          id: '0',
          value: '同步任务',
          label: '同步任务'
        },
        {
          id: '1',
          value: '统计任务',
          label: '统计任务'
        },
        {
          id: '2',
          value: '系统任务',
          label: '系统任务'
        }
      ],
      value: '',
      leixin: '',
      arr: [],
      arrlist: [],
      aaa: '',
      dialogVisible: false, // 控制弹窗 显示
      // 点击查看按钮  这条数据详细信息
      start: 1, // 默认显示第几页
      totalCount: 0, // 总条数，根据接口获取数据长度(注意：这里不能为空)
      pgeSizes: [10, 20, 30, 40], // 个数选择器（可修改）
      PageSize: 10 // 默认每页显示的条数（可修改）
      // 点击查看按钮  这条数据详细信息
    }
  },
  created() {
    this.lastTime = new Date().getTime()
  },
  mounted() {
    this.quartzTaskGetList()
  },
  methods: {
    yes_dele(scope) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      console.log(scope.row.id)
      const id = scope.row.id
      quartzTaskUpdate({ id: id, status: -1 }).then((res) => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.quartzTaskGetList()
      })
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    no_dele(scope) {
      this.$message('取消删除')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    yes_stop(scope) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      console.log(scope.row.id)
      const id = scope.row.id
      quartzTaskUpdate({ id: id, status: 0 }).then((res) => {
        this.$message({
          message: '暂停成功',
          type: 'success'
        })
        this.quartzTaskGetList()
      })
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    no_stop(scope) {
      this.$message('取消暂停')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    yes_play(scope) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      console.log(scope.row.id)
      const id = scope.row.id
      quartzTaskUpdate({ id: id, status: 2 }).then((res) => {
        this.$message({
          message: '恢复成功',
          type: 'success'
        })
        this.quartzTaskGetList()
      })
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    no_play(scope) {
      this.$message('取消恢复')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    yes_play(scope) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      console.log(scope.row.id)
      const id = scope.row.id
      quartzTaskUpdate({ id: id, status: 2 }).then((res) => {
        this.$message({
          message: '恢复成功',
          type: 'success'
        })
        this.quartzTaskGetList()
      })
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    no_play(scope) {
      this.$message('取消恢复')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    //
    yes_est(scope) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      console.log(scope.row.id)
      const id = scope.row.id
      quartzTaskUpdate({ id: id, status: 1 }).then((res) => {
        this.$message({
          message: '执行成功',
          type: 'success'
        })
        this.quartzTaskGetList()
      })
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    no_est(scope) {
      this.$message('取消执行')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    // 搜索按钮
    btn() {
      quartzTaskGetList({
        type: this.leixin,
        status: this.value === '' ? null : Number(this.value)
      }).then((res) => {
        this.arr = res.data
      })
    },
    handleEdit(info) {
      // 弹窗打开
      this.infos = info
      this.dialogVisible = true
      this.dialogInfo = info
      console.log(info)
    },
    rizhi(scope) {
      this.aaa = scope.row.id
      // quartzTimeQueryList({ quartzTaskId: this.aaa }).then((res) => {
      //   this.arrlist = res.data;
      // });
      this.quartzTimeQueryList(this.PageSize, this.start)
      this.centerDialogVisible = true
    },
    quartzTimeQueryList(n1, n2) {
      quartzTimeQueryList({ quartzTaskId: this.aaa, size: n1, start: n2 }).then(
        (res) => {
          this.arrlist = res.data
          this.totalCount = res.totalCount
        }
      )
    },
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val
      // 点击每页显示的条数时，显示第一页
      this.quartzTimeQueryList(val, 1)
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.start = 1
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.start = val

      // 切换页码时，要获取每页显示的条数
      this.quartzTimeQueryList(this.PageSize, (this.start = val))
    },
    handleDelete(index, row) {
      console.log(index, row)
    },
    quartzTaskGetList() {
      quartzTaskGetList().then((res) => {
        if (res.code === 200) {
          this.arr = res.data
          this.totalCount = res.totalCount
        } else if (res.code == 3) {
          Message({
            message: res.msg,
            type: 'error',
            duration: 5 * 1000
          })
          this.$router.push(`/login?redirect=${this.$route.fullPath}`)
        }
      })
    },
    change: function(index, row) {
      console.log(index, row, '---')
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    submitForm(infos) {
      this.$refs[infos].validate((valid) => {
        if (valid) {
          quartzEdit({
            cron: this.infos.cron,
            description: this.infos.description,
            id: this.infos.id
          }).then((res) => {
            if (res.code === 200) {
              this.quartzTaskGetList()
            } else if (res.code === 1) {
              Message({
                message: res.msg,
                type: 'error',
                duration: 1500
              })
            }
          })
        } else {
          this.$message({
            message: '请填写必填项！',
            type: 'warning',
            duration: 1500
          })
          this.dialogVisible = true
          return false
        }
      })
    }
  }
}
</script>
